{% extends 'base.html' %}
{% from 'bootstrap/pagination.html' import render_pagination %}

{% block title %}Manage Posts{% endblock %}

{% block content %}
    <div class="page-header">
        <h1>文章数:
            <small class="text-muted">{{ pagination.total }}</small>
            <span class="float-right"><a class="btn btn-primary btn-sm"
                                         href="{{ url_for('.new_post') }}">创建新的文章</a></span>
        </h1>
    </div>
    {% if posts %}
        <table class="table table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>文章标题</th>
                <th>文章分类</th>
                <th>日期</th>
                <th>评论数</th>
                <th>总文字数</th>
                <th>操作</th>
            </tr>
            </thead>
            {% for post in posts %}
                <tr>
                    <td>{{ loop.index + ((page - 1) * config.BLUELOG_MANAGE_POST_PER_PAGE) }}</td>
                    <td><a href="{{ url_for('blog.show_post', post_id=post.id) }}">{{ post.title }}</a></td>
                    <td>
                        <a href="{{ url_for('blog.show_category', category_id=post.category.id) }}">{{ post.category.name }}</a>
                    </td>
                    <td>{{ moment(post.timestamp).format('LL') }}</td>
                    <td>
                        <a href="{{ url_for('blog.show_post', post_id=post.id) }}#comments">{{ post.comments|length }}</a>
                    </td>
                    <td>{{ post.body|striptags|length }}</td>
                    <td>
                        <form class="inline" method="post"
                                {#                              next是为当提交表格可以回到当前页面#}
                              action="{{ url_for('.set_comment', post_id=post.id, next=request.full_path) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                            <button type="submit" class="btn btn-warning btn-sm">
                                {% if post.can_comment %}禁止{% else %}启动{% endif %} 评论
                            </button>
                        </form>
                        <a class="btn btn-info btn-sm" href="{{ url_for('.edit_post', post_id=post.id) }}">编辑</a>
                        <form class="inline" method="post"
                              action="{{ url_for('.delete_post', post_id=post.id, next=request.full_path) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                            <button type="submit" class="btn btn-danger btn-sm"
                                    onclick="return confirm('您确定删除这篇文章吗?');">删除
                            </button>
                        </form>
                    </td>
                </tr>
            {% endfor %}
        </table>
        <div class="page-footer">{{ render_pagination(pagination) }}</div>
    {% else %}
        <div class="tip"><h5>暂无文章!</h5></div>
    {% endif %}
{% endblock %}